<template>
  <div class="home-container">
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        slot="title"
        round
        class="search-btn"
        icon="search"
        type="info"
        size="small"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <!-- 频道列表 -->
    <!-- 通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in userChannel" :key="item.id" :title="item.name">
        <!-- 文章列表 -->
        <article-list :item="item" class=""></article-list>
      </van-tab>
      <!-- 下标签的作用：占位，当划到最后一个频道时，使其不被汉堡按钮遮住 -->
      <!-- 占位，不参与 van-tabs 的空间分配（空间被以上标签平均分配） -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn" @click="isChannelShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 点击汉堡按钮弹出层 -->
    <van-popup
      class="popup"
      v-model="isChannelShow"
      close-icon-position="top-right"
      closeable
      position="left"
      :style="{ height: '100%' }"
    >
      <!-- 我的频道列表入口
       @updateActive="onUpDateActive" ：子组件的事件updateActive绑定父组件的事件onUpDateActive
      -->
      <channel-edit
        :my-channel="userChannel"
        :active="active"
        @updateActive="onUpDateActive"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannel } from '@/api/user'
import articleList from './comment/article-list.vue'
import ChannelEdit from './comment/channel-edit.vue'
import { getItem } from '@/utils/storage'
import { mapState } from 'vuex'

export default {
  components: { articleList, ChannelEdit },
  data () {
    return {
      active: 0,
      userChannel: [],
      isChannelShow: false
    }
  },
  computed: {
    name () {
      return this.data
    },
    ...mapState(['user'])
  },
  created () {
    this.loadUserChannel()
  },
  mounted () {},
  methods: {
    // 加载用户频道
    async loadUserChannel () {
      try {
        // 真正展示首页频道列表
        let channels = []
        // 1、判断是否已登录
        if (this.user) {
          // 2、已登录  直接获取用户频道列表
          const { data } = await getUserChannel()
          // console.log(data);
          channels = data.data.channels
        } else {
          // 获取本地存储,  注意参数传入key
          const localChannels = await getItem('TOUTIAO_CHANNELS')
          // 3、未登录  判断本地是否有存储频道列表
          if (localChannels) {
            // 4、本地存储的有频道列表，直接获取；
            channels = localChannels
          } else {
            // 5、本地没有存储频道列表，获取默认频道列表
            const { data } = await getUserChannel()
            channels = data.data.channels
          }
        }
        this.userChannel = channels
      } catch (error) {
        this.$toast('获取用户频道失败')
      }
    },

    // 监听子组件发过来的消息
    onUpDateActive (index, ischannelshow = true) {
      // 更新激活的频道项
      this.active = index
      // 关闭频道管理弹出层
      this.isChannelShow = ischannelshow
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  /deep/ .van-nav-bar__title {
    //unset：不设置
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    height: 82px;
    .van-tab {
      border-right: 1px solid #edeff3;
      //如果标签较多时，不能让这些标签平分宽度
      // 为每一个标签设置最小宽度
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
      bottom: 8px;
    }
    .placeholder {
      //表示不参与空间分配
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902; //设置透明度
      i.toutiao {
        font-size: 33px;
      }
      .popup {
        .van-icon {
          z-index: 2002;
        }
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
